<template>
  <a-modal
    title="权限"
    :width="340"
    :maskClosable = "false"
    :visible="visible"
    :confirmLoading="loading"
    @ok="ok"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="layout">
        <a-form-item label="身份">
          <a-select :default-value="3" :value="model.status" style="width: 180px" @change="handleStatusChange">
            <a-select-option v-for="(item, index) in statusOptions" :key="index" :value="item.value">
              {{item.title}}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="岗位">
          <a-select mode="multiple" :value="model.positions" style="width: 180px" :disabled="model.status !== 3"
                    @change="handlePositionChange">
            <a-select-option v-for="(item, index) in positionOptions" :key="index" :value="item.id">
              {{item.name}}
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import API from "@/api";

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    return {
      form: this.$form.createForm(this),
      statusOptions: [{value: 0, title: '停用'}, {value: 1, title: '人事处'}
      , {value: 2, title: '院领导'}, {value: 3, title: '系主任'}],
      positionOptions: [],
      layout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 7 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 13 }
        }
      }
    }
  },
  created () {
    API.Position.List().then((res) => {
      if (res) this.positionOptions = res.data
    })
  },
  methods: {
    handleStatusChange(selected) {
      this.model.status = selected
    },
    handlePositionChange(selected) {
      this.model.positions = selected
    },
    ok() {
      this.$emit('ok', this.model)
    }
  }
}
</script>
